package com.rhb.oa.news.web.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.*;
import java.text.*;
import org.apache.struts.action.*;

import com.rhb.oa.news.bean.News;
import com.rhb.oa.news.web.struts.form.NewsForm;
import com.rhb.oa.web.struts.action.OaBaseDispatchAction;

import com.rhb.ca.uzer.bean.Uzer;
import com.rhb.ca.web.util.HttpConnect;
import com.rhb.oa.news.bean.*;


public class NewsControlAction extends OaBaseDispatchAction {
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		//System.out.println("unspecified for defalut parameter!");
		return list(mapping,form,request,response);
	}

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		NewsForm eForm = (NewsForm)form;

		int allcount = this.getNewsBusiness().getAllCount_findByQuery(eForm.getNewsQuery());
		List list = null;
		if ( allcount != 0) {
			list = this.getNewsBusiness().findByQuery(eForm.getNewsQuery(),eForm.getStart(),eForm.getCount());
		}else{
			list = (List)new ArrayList();
		}

		eForm.setAllCount(allcount);
		eForm.setList(list);
		return mapping.findForward("list");
	}

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		News news = new News();	
		
		news.setMadeDate(new Date());
		Uzer uzer = null;
		uzer = HttpConnect.getUzer(request);
		if(uzer!=null){
			news.setMadePersonPK(uzer.getUzerPk());
			news.setMadePersonName(uzer.getUzerName());

		}
		
		NewsForm oform = (NewsForm)form;
		oform.setNews(news);
		
		return mapping.findForward("add");
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		NewsForm oform = (NewsForm)form;
		oform.setNews(this.getNewsBusiness().findByPk(oform.getNews().getNewsPk()));
		return mapping.findForward("edit");
	}
	
	public ActionForward view(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		NewsForm oform = (NewsForm)form;
		oform.setNews(this.getNewsBusiness().findByPk(oform.getNews().getNewsPk()));
		return mapping.findForward("view");
	}
	
	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		return mapping.findForward("del");
	}
	
 
}
